package com.gainloop.Api;

import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;
import org.json.JSONException;

import com.gainloop.Api.Entity.Help;
import com.gainloop.Api.Entity.User;
import com.gainloop.Api.Entity.Wish;

/**
 * @author owner
 * 
 */
public class Helps extends ApiAbstract {
	
	static private Helps _instance = null;
	
	public static Helps getInstance()
	{
		if(_instance == null) {
			_instance = new Helps();
		}
		
		return _instance;
	}

	public ApiResponseObject<Help> create(Help help)
			throws JSONRPCException {
		if (help.getId() != 0) {
			throw new ApiException("help id must not set");
		}
		if (help.getWishId() == 0) {
			throw new ApiException("wish id must set");
		}

		try {
			return new ApiResponseObject<Help>(GetClient().callJSONObject(
					"helps.create", help.toObject()), Help.class);
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

	public Help getById(long id) throws JSONRPCException {

		try {
			ApiResponseObject<Help> res = new ApiResponseObject<Help>(GetClient().callJSONObject(
					"helps.getById", id), Help.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

	public ArrayList<Help> getAllByWish(Wish wish) throws JSONRPCException {
		return getAllByWish(wish, 25);
	}

	public ArrayList<Help> getAllByWish(Wish wish, int limit)
			throws JSONRPCException {
		
		if (limit < 1 || limit > 50) {
			throw new ApiException("invalid limit");
		}
		if(wish.getId() == 0) {
			throw new ApiException("wish id must be set");
		}

		try {
			ApiResponseArray<Help> res = new ApiResponseArray<Help>(GetClient().callJSONObject(
					"helps.getAllByWish", wish.getId(), limit), Help.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();
			
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

	public ArrayList<Help> getAllByUser(User user) throws JSONRPCException {
		return getAllByUser(user, 25);
	}
	
	public ArrayList<Help> getAllByUser(User user, int limit)
			throws JSONRPCException {
		
		if (limit < 1 || limit > 50) {
			throw new ApiException("invalid limit");
		}
		if(user.getId() == 0) {
			throw new ApiException("user id must be set");
		}

		try {
			ApiResponseArray<Help> res = new ApiResponseArray<Help>(GetClient().callJSONObject(
					"helps.getAllByUser", user.getId(), limit), Help.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();
			
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

	public Help setHelpCanceled(Help help) throws JSONRPCException
	{
		return setHelpIgnored(help);
	}

	public Help setHelpIgnored(Help help) throws JSONRPCException
	{
		try {
			ApiResponseObject<Help> res =  new ApiResponseObject<Help>(GetClient().callJSONObject(
					"helps.setHelpIgnored", help.getId()), Help.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();			
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

	public Help setHelpResolved(Help help) throws JSONRPCException
	{
		try {
			ApiResponseObject<Help> res =  new ApiResponseObject<Help>(GetClient().callJSONObject(
					"helps.setHelpResolved", help.getId()), Help.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();			
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}

}
